<?php
include "../includes/common.php";
if ($islogin == 1) {} else {
    exit("<script language='javascript'>window.location.href='./login.php';</script>");
}

$act = isset($_GET['act']) ? daddslashes($_GET['act']) : null;

if (!checkRefererHost()) {
    exit('{"code":403}');
}

@header('Content-Type: application/json; charset=UTF-8');

switch ($act) {
    case 'userList':
        $sql = " 1=1";
        if (isset($_POST['dstatus']) && !empty($_POST['dstatus'])) {
            $dstatus = explode('_', $_POST['dstatus']);
            $sql .= " AND `{$dstatus[0]}`='{$dstatus[1]}'";
        }
        if (isset($_POST['value']) && !empty($_POST['value'])) {
            $sql .= " AND `{$_POST['column']}`='{$_POST['value']}'";
        }
        $offset = intval($_POST['offset']);
        $limit = intval($_POST['limit']);
        $total = $DB->getColumn("SELECT count(*) from pre_partner WHERE{$sql}");
        $list = $DB->getAll("SELECT * FROM pre_partner WHERE{$sql} order by id desc limit $offset,$limit");

        exit(json_encode(['total' => $total, 'rows' => $list]));
        break;
    case 'addUser':
        $key = random(32);
        $data = [
            'sign_key' => $key,
            'name' => trim($_POST['name']),
            'domain' => trim($_POST['domain']),
            'email' => trim($_POST['email']),
            'qq' => trim($_POST['qq']),
            'tel' => trim($_POST['tel']),
            'status' => intval($_POST['status']),
            'create_time' => 'NOW()',
        ];

        if (empty($data['name']) || empty($data['status'])) {
            exit('{"code":-1,"msg":"必填项不能为空！"}');
        }

        if (!empty($data['tel'])) {
            if ($DB->find('partner', '*', ['tel' => $data['tel']])) {
                exit('{"code":-1,"msg":"手机号已存在！"}');
            }
        }
        if (!empty($data['email'])) {
            if ($DB->find('partner', '*', ['email' => $data['email']])) {
                exit('{"code":-1,"msg":"邮箱已存在！"}');
            }
        }

        $id = $DB->insert('partner', $data);
        if ($id !== false) {
            if (!empty($_POST['password'])) {
                $password = getMd5Pwd(trim($_POST['password']), $id);
                $DB->update('partner', ['password' => $password], ['id' => $id]);
            }
            exit(json_encode(['code' => 0, 'id' => $id, 'sign_key' => $key]));
        } else {
            exit('{"code":-1,"msg":"添加合作商失败！' . $DB->error() . '"}');
        }
        break;
    case 'editUser':
        $id = intval($_GET['id']);
        $rows = $DB->getRow("select * from pre_partner where id='$id' limit 1");
        if (!$rows) {
            exit('{"code":-1,"msg":"当前合作商不存在！"}');
        }

        $data = [
            'name' => trim($_POST['name']),
            'domain' => trim($_POST['domain']),
            'email' => trim($_POST['email']),
            'qq' => trim($_POST['qq']),
            'tel' => trim($_POST['tel']),
            'status' => intval($_POST['status']),
            'update_time' => 'NOW()',
        ];

        if (empty($data['name']) || empty($data['status'])) {
            exit('{"code":-1,"msg":"必填项不能为空！"}');
        }

        if ($DB->update('partner', $data, ['id' => $id]) !== false) {
            if (!empty($_POST['password'])) {
                $password = getMd5Pwd(trim($_POST['password']), $id);
                $DB->update('partner', ['password' => $password], ['id' => $id]);
            }
            exit('{"code":0}');
        } else {
            exit('{"code":-1,"msg":"修改合作商信息失败！' . $DB->error() . '"}');
        }
        break;
    case 'delUser':
        $id = intval($_GET['id']);
        if ($DB->exec("DELETE FROM pre_partner WHERE id='$id'")) {
            exit('{"code":0}');
        } else {
            exit('{"code":-1,"msg":"删除合作商失败！' . $DB->error() . '"}');
        }
        break;
    case 'setUser':
        $id = intval($_POST['id']);
        $status = intval($_POST['status']);
        $sql = "UPDATE pre_partner SET status='$status' WHERE id='$id'";
        if ($DB->exec($sql) !== false) {
            exit('{"code":0,"msg":"修改状态成功！"}');
        } else {
            exit('{"code":-1,"msg":"修改状态失败[' . $DB->error() . ']"}');
        }
        break;
    case 'resetUser':
        $id = intval($_GET['id']);
        $key = random(32);
        $sql = "UPDATE pre_partner SET `sign_key`='$key' WHERE id='$id'";
        if ($DB->exec($sql) !== false) {
            exit('{"code":0,"msg":"重置密钥成功","key":"' . $key . '"}');
        } else {
            exit('{"code":-1,"msg":"重置密钥失败[' . $DB->error() . ']"}');
        }
        break;
}
